package com.itheima.entity;

import com.hzheima.util.ImageUtil;
import com.itheima.config.AppConfig;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Plan extends FIyingObject {

    public Plan() {
        img = ImageUtil.getImg("WJC.png");
        width = img.getWidth() / 2;
        height = img.getHeight() / 2;
        x = 200;
        y = 600;
        speed = 18;
    }

    public void moveToLeft() {
        x = x - speed;
        if (x < 0) {
            x = 0;
        }
    }

    public void moveToRight() {
        x = x + speed;
        if (x > AppConfig.APP_WIDTH - 10 - this.width) {
            x = AppConfig.APP_WIDTH - 10 - this.width;
        }
    }

    public void moveToForward() {
        y = y - speed;
        if (y < 0) {
            y = 0;
        }
    }

    public void moveToBackward() {
        y = y + speed;
        if (y > AppConfig.APP_HEIGHT- 10 - this.height) {
            y = AppConfig.APP_HEIGHT - 10 - this.height;
        }
    }

    ArrayList<Bullet> bullets = new ArrayList<>();

    int count = 0;

    public ArrayList<Bullet> getBullets() {
        return bullets;
    }

    /*
     * 发射子弹
     */
    public void shoot() {
        count++;
        if (count % 10 == 0) {
            //发射一颗子弹
            Bullet bullet = new Bullet(this.x, this.y);
            bullets.add(bullet);
            //count重置为0
            count = 0;
        }

        //让子弹移动
        for (int i = bullets.size() - 1; i >= 0; i--) {
            Bullet bullet = bullets.get(i);
            bullet.move();

            //如果子弹移出屏幕了，就从集合中把子弹删除
            if (bullet.y < -bullet.height) {
                bullets.remove(bullet);
            }
        }
    }

    public boolean isCrash(Ball ball) {
        boolean flag = ball.y < this.y + this.height && ball.y + ball.height > this.y
                && ball.x + ball.width > this.x && ball.x < this.x + this.width;
        //如果撞上了  改变图片
        if (flag) {
            changeImg();
        }
        return flag;
    }

    private void changeImg() {
        img = ImageUtil.getImg("boom.png");
    }
}
